package cn.botter.subject.domain.handler.subject.impl;

import cn.botter.subject.common.enums.IsDeletedEnum;
import cn.botter.subject.common.enums.SubjectInfoTypeEnum;
import cn.botter.subject.domain.convert.JudgeSubjectConverter;
import cn.botter.subject.domain.convert.MultipleSubjectConverter;
import cn.botter.subject.domain.entity.SubjectAnswerBO;
import cn.botter.subject.domain.entity.SubjectInfoBO;
import cn.botter.subject.domain.handler.subject.SubjectTypeHandler;
import cn.botter.subject.infra.basic.entity.SubjectJudge;
import cn.botter.subject.infra.basic.entity.SubjectMultiple;
import cn.botter.subject.infra.basic.service.SubjectJudgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class JudgeTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectJudgeService service;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入
        //获得info表的id即subjectId;
        Integer SubjectId = subjectInfoBO.getId();
        List<SubjectJudge> subjectJudgeList = new LinkedList<>();
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        if (CollectionUtils.isEmpty(optionList)){
            log.info("Subject.Radio.add options isEmpty!");
            return;
        }
        optionList.forEach(option->{
            SubjectJudge subjectJudge = JudgeSubjectConverter.INSTANCE.convertBoToJudge(option);
            subjectJudge.setSubjectId(SubjectId);
            subjectJudge.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            subjectJudgeList.add(subjectJudge);
        });

        service.add(subjectJudgeList);
    }

    @Override
    public SubjectInfoBO query(Integer id) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(id);
        List<SubjectJudge> result = service.queryByCondition(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConverter.INSTANCE.convertRadioToAnswerBO(result);


        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        subjectInfoBO.setOptionList(subjectAnswerBOList);
        return subjectInfoBO;
    }
}
